﻿
// 2018/6/20: 首个记录
// 2018/12/29: 改pragma once
// 2019/3/29: 添加注释
// 2019/10/11: 修改include
// 2020/1/13: 接口完备性
// 2020/5/15: 修正fromGeneralSample
// 2020/11/12: 支持Spadas7
// 2022/11/2: 支持Spadas8.4
// 2024/6/13: 基于新样本框架
// 2025/8/4: 改为继承SessionSample。改回单头文件

#ifndef MAN_TRIGGER_SAMPLE_V1_H
#define MAN_TRIGGER_SAMPLE_V1_H

#include "spadas.h"

namespace man_trigger_sample_v1
{
	using namespace spadas;

	struct ManTriggerSampleData
	{
		Bool triggers[16]; // 16路手动触发器是否激活

		ManTriggerSampleData()
		{
			for (UInt i = 0; i < 16; i++) triggers[i] = FALSE;
		}
	};

	class ManTriggerSample : public SessionSample
	{
	private:
		class ManTriggerSampleVars : public SessionSampleVars
		{
		public:
			SPADAS_VARS(ManTriggerSample, SessionSampleVars)
			ManTriggerSampleData data;

			Bool supportInterpolation() override
			{
				return FALSE;
			}

			SessionGeneralSample toGeneralSample()
			{
				SessionGeneralSample sample;
				sample.timestamp() = timestamp;
				sample.values() = Array<GeneralElement>(16);

				for (UInt i = 0; i < 16; i++)
				{
					sample.values()[i] = data.triggers[i] ? 1 : 0;
				}

				sample.significantCount() = 16;
				return sample;
			}

			Bool fromSample(String protocol, SessionSample sample) override
			{
				SessionGeneralSample generalSample;
				if (!sample.is<SessionGeneralSample>(generalSample)) return FALSE;

				timestamp = generalSample.timestamp();
				auto values = generalSample.values();

				if (protocol == "man-trigger-sample-v1")
				{
					if (values.size() != 16) return FALSE;

					for (UInt i = 0; i < 16; i++)
					{
						data.triggers[i] = values[i].value == 1;
					}

					return TRUE;
				}

				return FALSE;
			}
		};

	public:
		SPADAS_CLASS("man_trigger_sample_v1.ManTriggerSample", ManTriggerSampleVars)

		ManTriggerSample() : SessionSample(new ManTriggerSampleVars)
		{}
		ManTriggerSampleData *operator ->()
		{
			return &var()->data;
		}

		static String getProtocolName()
		{
			return "man-trigger-sample-v1";
		}
		SessionGeneralSample toGeneralSample()
		{
			return var()->toGeneralSample();
		}

		static Array<String> getProtocolNames()
		{
			return Array<String>::create(1, "man-trigger-sample-v1");
		}
		Bool fromGeneralSample(String protocol, SessionGeneralSample generalSample)
		{
			return fromSample(protocol, generalSample);
		}
	};
}

#endif
